package tcc.sglj.action;

import tcc.sglj.util.HibernateUtil;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ParentPackage;

import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;


import org.hibernate.Query;


import java.util.Iterator;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.ProjectionList;

import tcc.sglj.dao.*;

import tcc.sglj.model.*;
import tcc.sglj.util.*;

/**
 *
 * @author sled
 */
@ParentPackage(value = "json-default")
public class HelloAction {

    private String message;
    private List<UserTe> users;
    private UserTe user;
    // private List listar;
    private int total = 0;
    private int page = 1;
    private int records;
    private List rows;

//
    @Action(value = "run", results = {
        @Result(name = "sucess", location = "/index.jsp")
    })
    public String run() {
//        Session session = HibernateUtil.getSession();
//        Transaction tx = session.beginTransaction();
//
//        try {
//            UserTe user = new UserTe();
//            user.setName("zaine sled zsled sled zainejhon zaineSLED");
//            session.save(user);
//            tx.commit();
//        } catch (HibernateException e) {
//            this.setMessage(404);
//        }1
        this.setMessage("ok funcionando perfeitamente!");
        return "sucess";
    }

    @Action(value = "jsons", results =
    @Result(name = "success", type = "json"))
    public String jsons() {
//        HttpServletRequest rq = ServletActionContext.getRequest();
//        String name = rq.getParameter("name");

        Session session = HibernateUtil.getSession();

//        User user  = new User();
//        user.setName(name);
//
//        HibernateImplDao<User, Long> dao = new HibernateImplDao<User, Long>();
//
//        dao.setSession(session);
//
//        User u = (User)dao.makePersistent(user);
//
        Query query = session.createQuery("from User u");

        // this.setUsers(query.list());
        this.setUsers((List<UserTe>) query.list());

//        HibernateImplDao<UserTe, Long> dao = new HibernateImplDao<UserTe, Long>();
//        dao.setSession(session);
//        UserTe te = new UserTe();
//        te.setName("eu eu eu eu ");
//
//        UserTe e = (UserTe) dao.makePersistent(te);
        //     this.setMessage(this.getUsers().size());

        return "success";
    }

    @Action(value = "/listar", results =
    @Result(name = "success", type = "json", params = {"excludeProperties", "user, users, message"}))
    public String listUser() {
        try {
            Session session = HibernateUtil.getSession();

            List list = session.createCriteria(User.class).setProjection(Projections.projectionList()
                    .add(Projections.property("id"))
                    .add(Projections.property("name"))
                    .add(Projections.property("email"))
                    .add(Projections.property("cpf"))
                    .add(Projections.property("sign"))).list();
//            ProjectionList list = Projections.projectionList();
//              list.add(Projections.property("id"));
//            list.add(Projections.property("name"));
//            list.add(Projections.property("email"));
//            list.add(Projections.property("cpf"));
//            list.add(Projections.property("sign"));

         //   critera.setProjection(list);

            setRows(list);
            setRecords(list.size());

        } catch (Exception e) {

            setMessage(e.getCause().toString());
            //     out.println(e.getMessage());
        }
        return "success";
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the users
     */
    public List<UserTe> getUsers() {
        return users;
    }

    /**
     * @param users the users to set
     */
    public void setUsers(List<UserTe> users) {
        this.users = users;
    }

    /**
     * @return the user
     */
    public UserTe getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(UserTe user) {
        this.user = user;
    }

    /**
     * @return the rows
     */
    public List getRows() {
        return rows;
    }

    /**
     * @param rows the rows to set
     */
    public void setRows(List rows) {
        this.rows = rows;
    }

    /**
     * @return the total
     */
    public int getTotal() {
        return total;
    }

    /**
     * @param total the total to set
     */
    public void setTotal(int total) {
        this.total = total;
    }

    /**
     * @return the page
     */
    public int getPage() {
        return page;
    }

    /**
     * @param page the page to set
     */
    public void setPage(int page) {
        this.page = page;
    }

    /**
     * @return the records
     */
    public int getRecords() {
        return records;
    }

    /**
     * @param records the records to set
     */
    public void setRecords(int records) {
        this.records = records;
    }
}
